package com.ludomancy.sora;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import com.ludomancy.gamelib.Widget;
import com.ludomancy.gamelib.WorldScreenProjection;

public class MainWidget extends Widget
{
	private WorldScreenProjection projection;
	
	public MainWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
		projection = new WorldScreenProjection(getWidth(), getHeight(), Game.WIDTH, Game.HEIGHT);
	}
		
	public void update()
	{
		if(Game.game != null)
		{
			Game.game.update();
		}
	}
		
	public void paint(Graphics g)
	{
		g.setColor(new Color(0xFDFDFD));
		g.fillRect(0, 0, getWidth(), getHeight());
//		g.drawImage(ResourceManager.getImage("background.png"), 0, 0, null);
				
		// paint objects
		Game.game.simulation.paint(g, projection);
		
		// paint floor
		int[] floor = projection.worldToScreen(0, Game.FLOOR);
		g.drawLine(0, floor[1], getWidth(), floor[1]);
				
		// paint character
		int[] pos = projection.worldToScreen(Game.game.player.px, Game.game.player.py);
		int diameter = projection.worldToScreen(Game.game.player.radii*2, 0)[0];
		g.setColor(Game.game.angry ? Color.RED : Color.BLACK);
		g.fillOval(pos[0]-diameter/2, pos[1]-diameter/2, diameter, diameter);
	}
	
	public void mouseDown(Point pos)
	{
		Game.game.angry = true;
	}
	
	public void mouseUp(Point pos)
	{
		Game.game.angry = false;
	}
	
	public void mouseMoved(Point point)
	{
		Game.game.setTarget(projection.screenToWorldX(point.x), projection.screenToWorldY(point.y));
	}
	
	public void onDrag(Point point, Object object)
	{
		Game.game.setTarget(projection.screenToWorldX(point.x), projection.screenToWorldY(point.y));
	}
}
